package com.xinsoft.config.mqtt;//package com.hyopt.modules.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @Author Li9527
 * @Description 项目启动 监听主题
 * @Date 2023/4/12 16:14
 **/
@Slf4j
@Component
public class MQTTListener implements ApplicationListener<ContextRefreshedEvent> {

    private final MQTTConnect server;

    @Autowired
    public MQTTListener(MQTTConnect server) {
        this.server = server;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            server.setMqttClient("", "", new Callback());
            server.sub("v1/devices/me/telemetry");
        } catch (MqttException e) {
            log.error(e.getMessage(), e);
        }
    }
}